home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
language
/
iconv8_e.arc
/
DOCS.ARC
/
TR90-6.DOC
< prev
next >
Wrap
Text File
|
1985-11-20
|
20KB
|
793 lines
An Overview of Version 8 the Icon
Programming Language*
Ralph E. Griswold
TR 90-6a
January 1, 1990; last revised February 13, 1990
Department of Computer Science
The University of Arizona
Tucson, Arizona 85721
*This work was supported by the National Science Foundation under
Grant CCR-8713690.
An Overview of Version 8 the Icon
Programming Language
1.__Introduction
Icon is a high-level programming language with extensive
facilities for processing strings and structures. Icon has
several novel features, including expressions that may produce
sequences of results, goal-directed evaluation that automatically
searches for a successful result, and string scanning that allows
operations on strings to be formulated at a high conceptual
level.
Icon emphasizes high-level string processing and a design phi-
losophy that allows ease of programming and short, concise pro-
grams. Storage allocation and garbage collection are automatic in
Icon, and there are few restrictions on the sizes of objects.
Strings, lists, and other structures are created during program
execution and their size does not need to be known when a program
is written. Values are converted to expected types automati-
cally; for example, numeral strings read in as input can be used
in numerical computations without explicit conversion. Icon has
an expression-based syntax with reserved words; in appearance,
Icon programs resemble those of Pascal and C.
Although Icon has extensive facilities for processing strings
and structures, it also has a full repertoire of computational
facilities. It is suitable for a wide variety of applications.
Some examples are:
+ text analysis, editing, and formatting
+ document formating
+ artificial intelligence
+ expert systems
+ rapid prototyping
+ symbolic mathematics
+ text generation
+ data laundry
There are public-domain implementations of Icon for the Amiga,
the Atari ST, the Macintosh under MPW, MS-DOS, MVS, OS/2, many
- 1 -
UNIX systems, VM/CMS, and VMS. There also is a commercial imple-
mentation of an enhanced version of Icon for the Macintosh [1].
The remainder of this report briefly describes the highlights
of Icon. For a complete description, see [2,3].
2.__Expression_Evaluation
2.1__Conditional_Expressions
In Icon there are conditional expressions that may succeed and
produce a result, or may fail and not produce any result. An
example is the comparison operation
i > j
which succeeds (and produces the value of j) provided that the
value of i is greater than the value of j, but fails otherwise.
Similarly,
i > j > k
succeeds if the value of j is between i and k.
The success or failure of conditional operations is used
instead of Boolean values to drive control structures in Icon. An
example is
if i > j then k := i else k := j
which assigns the value of i to k if the value of i is greater
than the value of j, but assigns the value of j to k otherwise.
The usefulness of the concepts of success and failure is
illustrated by find(s1,s2), which fails if s1 does not occur as a
substring of s2. Thus
if i := find("or",line) then write(i)
writes the position at which "or" occurs in line, if it occurs,
but does not write a value if it does not occur.
Many expressions in Icon are conditional. An example is
read(), which produces the next line from the input file, but
fails when the end of the file is reached. The following expres-
sion is typical of programming in Icon and illustrates the
integration of conditional expressions and conventional control
structures:
while line := read() do
write(line)
This expression copies the input file to the output file.
- 2 -
If an argument of a function fails, the function is not
called, and the function call fails as well. This ``inheritance''
of failure allows the concise formulation of many programming
tasks. Omitting the optional do clause in while-do, the previous
expression can be rewritten as
while write(read())
2.2__Generators
In some situations, an expression may be capable of producing
more than one result. Consider
sentence := "Store it in the neighboring harbor"
find("or",sentence)
Here "or" occurs in sentence at positions 3, 23, and 33. Most
programming languages treat this situation by selecting one of
the positions, such as the first, as the result of the expres-
sion. In Icon, such an expression is a generator and is capable
of producing all three positions.
The results that a generator produces depend on context. In a
situation where only one result is needed, the first is produced,
as in
i := find("or",sentence)
which assigns the value 3 to i.
If the result produced by a generator does not lead to the
success of an enclosing expression, however, the generator is
resumed to produce another value. An example is
if (i := find("or",sentence)) > 5 then write(i)
Here the first result produced by the generator, 3, is assigned
to i, but this value is not greater than 5 and the comparison
operation fails. At this point, the generator is resumed and pro-
duces the second position, 23, which is greater than 5. The com-
parison operation then succeeds and the value 23 is written.
Because of the inheritance of failure and the fact that com-
parison operations return the value of their right argument, this
expression can be written in the following more compact form:
write(5 < find("or",sentence))
Goal-directed evaluation is inherent in the expression evalua-
tion mechanism of Icon and can be used in arbitrarily complicated
situations. For example,
- 3 -
find("or",sentence1) = find("and",sentence2)
succeeds if "or" occurs in sentence1 at the same position as and
occurs in sentence2.
A generator can be resumed repeatedly to produce all its
results by using the every-do control structure. An example is
every i := find("or",sentence)
do write(i)
which writes all the positions at which "or" occurs in sentence.
For the example above, these are 3, 23, and 33.
Generation is inherited like failure, and this expression can
be written more concisely by omitting the optional do clause:
every write(find("or",sentence))
There are several built-in generators in Icon. One of the most
frequently used of these is
i to j
which generates the integers from i to j. This generator can be
combined with every-do to formulate the traditional for-style
control structure:
every k := i to j do
f(k)
Note that this expression can be written more compactly as
every f(i to j)
There are a number of other control structures related to gen-
eration. One is alternation,
expr1 | expr2
which generates the results of expr1 followed by the results of
expr2. Thus
every write(find("or",sentence1) | find("or",sentence2))
writes the positions of "or" in sentence1 followed by the posi-
tions of "or" in sentence2. Again, this sentence can be written
more compactly by using alternation in the second argument of
find():
every write(find("or",sentence1 | sentence2))
- 4 -
Another use of alternation is illustrated by
(i | j | k) = (0 | 1)
which succeeds if any of i, j, or k has the value 0 or 1.
Procedures can be used to add